package Service.Admin.Imp;

import Dao.Admin.GradingManagementDao;
import Dao.DaoFactory;
import Dao.Role.ReplyGroupDao;
import Entity.MyTableData;
import Entity.ReplyGroup;
import Entity.Role.Role;
import Entity.Role.Teacher;
import Service.Admin.GradingManagementService;
import Util.DBUtil;

import java.sql.Connection;
import java.util.*;

public class GradingManagementServiceImp implements GradingManagementService {
    private GradingManagementDao gradingManagementDao;
    private Connection conn;

    public GradingManagementServiceImp() {
        conn = DBUtil.getConnByDruid();
        gradingManagementDao = DaoFactory.getInstance("gradingManagementDao");
    }

    /**
     * 新增评阅教师角色
     */
    @Override
    public Integer reviewerAssign(String tno) {
        int code = 1002;
        Integer res = gradingManagementDao.reviewerQuery(tno, conn);
        if (res == 0) {//该评阅人不存在,执行插入操作
            Integer res1 = gradingManagementDao.reviewerAssign(tno, conn);
            if (res1 == 1) {//插入成功
                code = 1001;
            }
        } else {//该评阅人已存在
            code = 1003;
        }
        DBUtil.closeConn(conn);
        return code;
    }

    /**
     * 新增答辩组教师角色
     *
     * @param tno 教师学号
     * @return Integer选项支
     */
    public Integer replyReviewerAssign(String tno) {
        int code = 1002;
        Integer res = gradingManagementDao.replyReviewerQuery(tno, conn);
        if (res == 0) {//该答辩组教师不存在，执行插入操作
            Integer res1 = gradingManagementDao.replyReviewerAssign(tno, conn);
            if (res1 == 1) {//插入成功
                code = 1001;
            }
        } else {//该答辩组教师已存在

            code = 1003;
        }
        DBUtil.closeConn(conn);
        return code;
    }

    /**
     * 新增评阅教师表师生对信息
     */
    public Integer gradingAdd(String tno, String sno) {
        int code = 1002;
        Integer res = gradingManagementDao.stuReviewerQuery(sno, conn);
        if (res == 0) {//该学生不存在评阅教师，执行分配操作
            Integer res1 = gradingManagementDao.gradingAdd(tno, sno, conn);
            if (res1 == 1) {//插入成功
                code = 1001;
            }

        } else {//该学生存在评阅教师
            code = 1003;
        }
        DBUtil.closeConn(conn);
        return code;
    }


    /**
     * 按类型查询教师角色
     */
    @Override
    public MyTableData reviewerAllQuery(Integer type) {
        List<Role> roles = gradingManagementDao.reviewerAllQuery(type, conn);
        DBUtil.closeConn(conn);
        return new MyTableData(0, "", roles.size(), roles);
    }

    @Override
    public MyTableData getTeacherByRole(String roleName) {
        List<Teacher> teachers = gradingManagementDao.getAllByRole(roleName, conn);
        MyTableData myTableData = new MyTableData(0, "", teachers.size(), teachers);
        DBUtil.closeConn(conn);
        return myTableData;
    }


}
